Server allowing JOIN on NULL values in certain cases if query includes ORDER BY clause
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Drizzle |
Fix Released
|
Medium
|
PrafullaT | ||
Dexter |
Fix Released
|
Medium
|
PrafullaT |
Bug Description
The server is allowing a JOIN on NULL values for certain cases if an ORDER BY clause is included.
From the test case:
SELECT table2 .`col_int` field3 FROM i table1 JOIN b table2 ON table1 .`col_int_key` = table2 .`col_int_key` WHERE table1 .`pk` IN ( 4 ) ORDER BY field3
vs.
SELECT table2 .`col_int` field3 FROM i table1 JOIN b table2 ON table1 .`col_int_key` = table2 .`col_int_key` WHERE table1 .`pk` IN ( 4 ) /* TRANSFORM_
Produces this diff of result sets:
# 2010-05-26T15:38:30 --- /tmp//randgen10
# 2010-05-26T15:38:30 +++ /tmp//randgen10
# 2010-05-26T15:38:30 @@ -1 +0,0 @@
# 2010-05-26T15:38:30 -7
Here we can see what happened (the full result row):
SELECT table2 .`col_int` field3, table1.col_int_key, table2.col_int_key, table1.pk FROM i table1 JOIN b table2 ON table1 .`col_int_key` = table2 .`col_int_key` WHERE table1 .`pk` IN ( 4 ) ORDER BY field3;
+------
| field3 | col_int_key | col_int_key | pk |
+------
| 7 | NULL | NULL | 4 |
+------
That JOIN should not have happened. If we don't have the ORDER BY clause, the query properly returns 0 rows.
EXPLAIN output:
query with ORDER BY
# |1|SIMPLE|
# |1|SIMPLE|
# */
query without ORDER BY
# |1|SIMPLE|
# |1|SIMPLE|
# */
Related branches
Changed in drizzle: | |
importance: | Undecided → Medium |
status: | New → Confirmed |
Changed in drizzle: | |
assignee: | nobody → Prafulla Tekawade (prafulla-tekawade) |
Changed in drizzle: | |
status: | Confirmed → Fix Committed |
Test case:
Run and observe the difference in result sets between query 0 and query 1
#/* Server0: Drizzle 2010.05.1561 */
--disable_warnings
DROP TABLE IF EXISTS `i`;
CREATE TABLE `i` (
`col_int_key` int DEFAULT NULL,
`pk` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk`),
KEY `col_int_key` (`col_int_key`)
) ENGINE=InnoDB;
ALTER TABLE `i` DISABLE KEYS; 1),(-1066663936 ,2),(NULL, 3),(NULL, 4),(-1583808512 ,5),(NULL, 6),(NULL, 7),(NULL, 8),(0,9) ,(-219152384, 10);
INSERT INTO `i` VALUES (-480247808,
ALTER TABLE `i` ENABLE KEYS;
DROP TABLE IF EXISTS `b`;
CREATE TABLE `b` (
`col_int_key` int DEFAULT NULL,
`col_int` int DEFAULT NULL,
`pk` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk`),
KEY `col_int_key` (`col_int_key`)
) ENGINE=InnoDB;
ALTER TABLE `b` DISABLE KEYS;
INSERT INTO `b` VALUES (NULL,7,1);
ALTER TABLE `b` ENABLE KEYS;
--enable_warnings
#/* Begin test case for query 0 */
SELECT table2 .`col_int` field3
FROM i table1 JOIN b table2 ON table1 .`col_int_key` = table2 .`col_int_key`
WHERE table1 .`pk` IN ( 4 )
ORDER BY field3 ;
#/* End of test case for query 0 */
#/* Begin test case for query 1 */
SELECT table2 .`col_int` field3 OUTCOME_ UNORDERED_ MATCH */ ;
FROM i table1 JOIN b table2 ON table1 .`col_int_key` = table2 .`col_int_key`
WHERE table1 .`pk` IN ( 4 ) /* TRANSFORM_
#/* End of test case for query 1 */
DROP TABLE i;
DROP TABLE b;